package rabbitmq.simple;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

/**
 * 生产者
 */

public class ProducerDemo {
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        //连接信息
        factory.setHost("115.159.5.204");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/");
        //建立连接，创建信道
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        /**
         * 不声明交换机，使用内置交换机
         *
         * 声明队列
         * Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
         *                                  Map<String, Object> arguments) throws IOException;
         * queue：队列名称
         * durable：是否持久化
         * exclusive：是否被独占
         * autoDelete：是否自动删除
         * arguments：其他参数
         */
        channel.queueDeclare("hello",true,false,false,null);

        /**
         * 发送消息
         * void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body)
         *             throws IOException;
         * exchange：交换机名称
         * routingKey：路由键
         * props：属性配置
         * body：消息内容
         */
        for (int i =0; i < 10; i++){
            String msg = "Hello~" + i;
            channel.basicPublish("", "hello", null, msg.getBytes());
        }
        System.out.println("消息发送成功!");
        //关闭连接
        channel.close();
        connection.close();
    }
}
